package guda.house.web.action.sys;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.SmsBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.form.SmsEditForm;
import guda.house.biz.form.SmsForm;
import guda.house.biz.query.SmsQuery;
import guda.house.biz.security.AppUtil;
import guda.house.biz.util.ErrorCode;
import guda.house.dal.dataobject.SmsDO;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by well on 16/3/2.
 */
@Controller
public class SmsAction {

    @Autowired
    private SmsBiz smsBiz;

    @RequestMapping(value = "sys/sms/list.htm", method = RequestMethod.GET)
    public String list(HttpServletRequest request, ModelMap modelMap) {
        int pageId = RequestUtil.getInt(request, "pageNo");
        int pageSize = RequestUtil.getInt(request, "pageSize");
        SmsQuery smsQuery = new SmsQuery();
        smsQuery.setPageNo(pageId);
        smsQuery.setPageSize(pageSize);
        smsQuery.setVillageIdList(AppUtil.getVillageIdList());
        modelMap.put("query",smsQuery);
        BizResult bizResult = smsBiz.list(smsQuery);
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            return "sys/sms/list.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }
    }

    @RequestMapping(value = "sys/sms/edit.htm", method = RequestMethod.GET)
    public String edit(HttpServletRequest request, ModelMap modelMap, SmsEditForm smsEditForm,
                       BindingResult result, Map<String,Object> model) {
        BizResult bizResult = smsBiz.detail(smsEditForm.getSmsId());
        if (bizResult.success) {
            modelMap.putAll(bizResult.data);
            smsEditForm.initForm(((SmsDO)bizResult.data.get("smsDO")));
            return "sys/sms/edit.vm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }



    @RequestMapping(value = "sys/sms/create.htm", method = RequestMethod.GET)
    public String create(HttpServletRequest request, ModelMap modelMap, SmsForm smsForm,
                         BindingResult result) {
        return "sys/sms/create.vm";
    }

    @RequestMapping(value = "sys/sms/doCreate.htm", method = RequestMethod.POST)
    public String doCreate(HttpServletRequest request, ModelMap modelMap,@Valid SmsForm smsForm,
                           BindingResult result) {
        if (result.hasErrors()) {
            return "sys/sms/create.vm";
        }
        SmsDO smsDO = smsForm.toDO();
        BizResult bizResult = smsBiz.create(smsDO);
        if (bizResult.success) {
            return "redirect:/sys/sms/list.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/sms/doUpdate.htm", method = RequestMethod.POST)
    public String doUpdate(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap,@Valid SmsEditForm smsEditForm,
                           BindingResult result, Map<String,Object> model) {
        if (result.hasErrors()) {
            return "sys/sms/edit.vm";
        }
        SmsDO smsDO = smsEditForm.toDO();
        BizResult bizResult = smsBiz.update(smsDO);
        if (bizResult.success) {
            return "redirect:/sys/sms/result.htm";
        } else {
            return "redirect:/error.htm?code="+bizResult.code;
        }

    }

    @RequestMapping(value = "sys/sms/doDelete.htm")
    public void doDelete(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) {
        long id = RequestUtil.getLong(request, "id");
        BizResult bizResult = smsBiz.delete(id);
        JsonResult result = JsonResult.getInstance();
        if (bizResult.success) {
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

    }
}
